package com.huike.clues.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huike.clues.domain.TbActivity;
import com.huike.clues.domain.dto.TbActivityDTO;
import com.huike.clues.domain.dto.mydto.ActivityQueryDTO;
import com.huike.clues.service.TbActivityService;
import com.huike.clues.mapper.TbActivityMapper;
import com.huike.common.constant.Constants;
import com.huike.common.utils.uuid.UUIDUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 93238
 * @description 针对表【tb_activity(活动管理)】的数据库操作Service实现
 * @createDate 2023-10-12 06:35:46
 */

/**
 * 活动管理业务实现类
 */
@Service
public class TbActivityServiceImpl extends ServiceImpl<TbActivityMapper, TbActivity>
        implements TbActivityService {

    /**
     * 新增活动管理
     *
     * @param tbActivityDTO
     * @return
     */
    @Override
    public boolean addTbActivity(TbActivityDTO tbActivityDTO) {
        TbActivity activity = BeanUtil.toBean(tbActivityDTO, TbActivity.class);
        activity.setCode(UUIDUtils.getUUID());
        return this.save(activity);
    }

    /**
     * 修改活动管理
     *
     * @param tbActivityDTO
     * @return
     */
    @Override
    public boolean updateTbActivity(TbActivityDTO tbActivityDTO) {
        TbActivity activity = BeanUtil.toBean(tbActivityDTO, TbActivity.class);
        return this.updateById(activity);
    }

    /**
     * 分页查询活动管理信息
     *
     * @param activityQueryDTO
     * @return
     */
    @Override
    public Map<String, Object> selectTbActivityList(ActivityQueryDTO activityQueryDTO) {

        Page<TbActivity> page = new Page(activityQueryDTO.getPageNum(), activityQueryDTO.getPageSize());

        LambdaQueryChainWrapper<TbActivity> wrapper = this.lambdaQuery()
                .like(StringUtils.isNotBlank(activityQueryDTO.getCode()), TbActivity::getCode, activityQueryDTO.getCode())
                .eq(StringUtils.isNotBlank(activityQueryDTO.getChannel()), TbActivity::getChannel, activityQueryDTO.getChannel())
                .between(ObjectUtils.isNotEmpty(activityQueryDTO.getParams().get("beginTime")) && ObjectUtils.isNotEmpty(activityQueryDTO.getParams().get("endTime")), TbActivity::getBeginTime, activityQueryDTO.getParams().get("beginTime"), activityQueryDTO.getParams().get("endTime"))
                .between(ObjectUtils.isNotEmpty(activityQueryDTO.getParams().get("beginCreateTime")) && ObjectUtils.isNotEmpty(activityQueryDTO.getParams().get("endCreateTime")), TbActivity::getCreateTime, activityQueryDTO.getParams().get("beginCreateTime"), activityQueryDTO.getParams().get("endCreateTime"))
                .orderByDesc(TbActivity::getCreateTime);
        // 分页数据

        page = wrapper.page(page);


        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("total", page.getTotal());
        // 通过数据2
        Integer passCount = this.lambdaQuery().eq(TbActivity::getStatus, Constants.TBACTIVITY_PASS).count();
        paramMap.put("pass", passCount);
        // 拒绝数据3
        Integer rejectCount = this.lambdaQuery().eq(TbActivity::getStatus, Constants.TBACTIVITY_REJECT).count();
        paramMap.put("reject", rejectCount);
        // 提交1
        Integer toAuditCount = this.lambdaQuery().eq(TbActivity::getStatus, Constants.TBACTIVITY_TOAUDIT).count();
        paramMap.put("toAudit", toAuditCount);
        // 完成4
        Integer finishCount = this.lambdaQuery().eq(TbActivity::getStatus, Constants.TBACTIVITY_FINISH).count();
        paramMap.put("finish", finishCount);

        // 返回数据
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("page", page);
        resultMap.put("paramMap", paramMap);

        return resultMap;
    }

    /**
     * 获取状态为为2的渠道活动列表
     *
     * @param channel
     * @return
     */
    @Override
    public List<TbActivity> selectTbActivityByStatus(Long channel) {
        return this.list(Wrappers.<TbActivity>lambdaQuery().eq(TbActivity::getStatus, 2));
    }

    /**
     * 删除活动管理
     *
     * @param ids
     * @return
     */
    @Override
    public boolean delectTbActivity(Long[] ids) {
        return removeByIds(Arrays.asList(ids));
    }

    /**
     * 查询活动管理详细信息
     *
     * @param id
     * @return
     */
    @Override
    public TbActivity selectTbActivity(Long id) {
        return this.getById(id);
    }
}




